package com.yunjian.core.basic.service.impl;

import java.text.MessageFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunjian.core.basic.mapper.UserLogMapper;
import com.yunjian.core.basic.model.UserLog;
import com.yunjian.core.basic.model.UserLogExample;
import com.yunjian.core.basic.service.SequenceService;
import com.yunjian.core.basic.service.UserLogService;

@Service
public class UserLogServiceImpl implements UserLogService{
	
	//private final static Logger logger = LoggerFactory.getLogger(SUserServiceImpl.class);
	
	@Autowired
	private UserLogMapper mapper;
	
	@Autowired
	private SequenceService sequenceService;

	@Override
	public PageInfo<UserLog> selectPagedByExample(int page, int rows,
			UserLogExample example) {
		PageHelper.startPage(page, rows);
		PageInfo<UserLog> pageInfo = new PageInfo<>(mapper.selectByExample(example));
		return pageInfo;
	}


	@Override
	public void info(String userId, String ip, String busiType, String format,
			Object... arr) {
		UserLog ul = new UserLog();
		ul.setId(sequenceService.genCommonFlowNo(null));
		ul.setIp(ip);
		ul.setLevel("INFO");
		String msg = format;
		if (arr != null && arr.length > 0) {
			msg = MessageFormat.format(format, arr);
		}
		ul.setMessage(msg);
		ul.setSysNo("01");
		ul.setType(busiType == null ? "COMMON" : busiType);
		ul.setUserId(userId);
		ul.setMessage(msg);
		mapper.insert(ul);
	}


	@Override
	public void info(String userId, String ip, String format, Object... arr) {
		info(userId, ip, null, format, arr);
	}


	@Override
	public void info(String userId, String ip, String msg) {
		info(userId, ip, null, msg, new Object[]{});
	}
	@Override
	public void warn(String userId, String ip, String busiType, String format,
			Object... arr) {
		UserLog ul = new UserLog();
		ul.setId(sequenceService.genCommonFlowNo(null));
		ul.setIp(ip);
		ul.setLevel("INFO");
		String msg = format;
		if (arr != null && arr.length > 0) {
			msg = MessageFormat.format(format, arr);
		}
		ul.setMessage(msg);
		ul.setSysNo("01");
		ul.setType(busiType == null ? "COMMON" : busiType);
		ul.setUserId(userId);
		ul.setMessage(msg);
		mapper.insert(ul);
	}
	
	
	@Override
	public void warn(String userId, String ip, String format, Object... arr) {
		info(userId, ip, null, format, arr);
	}
	
	
	@Override
	public void warn(String userId, String ip, String msg) {
		info(userId, ip, null, msg, new Object[]{});
	}
	
	@Override
	public void error(String userId, String ip, String busiType, String format,
			Object... arr) {
		UserLog ul = new UserLog();
		ul.setId(sequenceService.genCommonFlowNo(null));
		ul.setIp(ip);
		ul.setLevel("INFO");
		String msg = format;
		if (arr != null && arr.length > 0) {
			msg = MessageFormat.format(format, arr);
		}
		ul.setMessage(msg);
		ul.setSysNo("01");
		ul.setType(busiType == null ? "COMMON" : busiType);
		ul.setUserId(userId);
		ul.setMessage(msg);
		mapper.insert(ul);
	}
	
	
	@Override
	public void error(String userId, String ip, String format, Object... arr) {
		info(userId, ip, null, format, arr);
	}
	
	
	@Override
	public void error(String userId, String ip, String msg) {
		info(userId, ip, null, msg, new Object[]{});
	}




	
}
